﻿using System;
using System.Data;
using Dng.DataFactory;
using Dng.Utilities;
using ShippingManage.DataAccessor.Entities;

namespace ShippingManage.Models
{
	public class VienDongShippingModel
	{
		//public VienDongShipping Get(Guid id)
		//{
		//  VienDongShipping result = null;
		//  using (var data = new ShippingManageContainer())
		//  {
		//    result = data.VienDongShippings.Where(o => o.Id == id).FirstOrDefault();
		//  }
		//  return result;
		//}
		//public List<VienDongShipping> Get(int page, int itemPerPage, out int totalRecord)
		//{
		//  List<VienDongShipping> result = null;
		//  using (var data = new ShippingManageContainer())
		//  {
		//    totalRecord = data.VienDongShippings.Count();
		//    result = (from obj in data.VienDongShippings
		//              select obj).Skip(page * itemPerPage).Take(itemPerPage).ToList();
		//  }
		//  return result;
		//}

		#region Load information from database
		// Load object data from database
		public VienDongShipping Get(Guid id)
		{
			VienDongShipping result = null;
			ProviderFactory dataPF = new ProviderFactory();
			dataPF.OpenConnection();
			try
			{
				result = Get(dataPF, id);
			}
			finally
			{
				dataPF.CloseConnection();
				dataPF.Dispose();
				dataPF = null;
			}
			return result;
		}

		// Load object data from database
		public VienDongShipping Get(ProviderFactory dataPF, Guid id)
		{
			var cmd = dataPF.CreateCommand("SELECT * FROM VienDongShippings WHERE Id=@Id");
			cmd.Parameters.Add(dataPF.CreateParameter("@Id", DbType.StringFixedLength, Services.I.G2S(id)));
			return dataPF.FillSingle<VienDongShipping>(cmd.ExecuteReader());
			// TO DO: Your validate code here!

			//IDbDataAdapter adapter = dataPF.CreateDataAdapter("SELECT * FROM VienDongShippings WHERE Id=@Id");
			//adapter.SelectCommand.Parameters.Add(dataPF.CreateParameter("@Id", DbType.Guid, _Id));
			//try
			//{
			//  DataSet ds = new DataSet();
			//  adapter.Fill(ds);
			//  if (ds.Tables[0].Rows.Count > 0)
			//    Fill(ds.Tables[0].Rows[0]);
			//  else
			//    throw new InformationNotFoundException("Not found.");
			//}
			//catch (Exception ex)
			//{
			//  throw ex;
			//}
		}

		//// Fill data into objects' attributes
		//public void Fill(DataRow data)
		//{
		//  try
		//  {
		//    _Id = (Guid)data["Id"];
		//    _DateCreated = (DateTime)data["DateCreated"];
		//    _SenderId = (Guid)data["SenderId"];
		//    _RecepientId = (Guid)data["RecepientId"];
		//    _Created = (DateTime)data["Created"];
		//    _CreatedId = (Guid)data["CreatedId"];
		//    _Modified = (DateTime)data["Modified"];
		//    _ModifiedId = (Guid)data["ModifiedId"];
		//    _TrackNumber = (string)data["TrackNumber"];
		//    _ServiceType = (short)data["ServiceType"];
		//    _TotalPakage = (string)data["TotalPakage"];
		//    _Height = (short)data["Height"];
		//    _Width = (short)data["Width"];
		//    _Length = (short)data["Length"];
		//    _DeclaredValue = (decimal)data["DeclaredValue"];
		//    _InsuranceAmount = (decimal)data["InsuranceAmount"];
		//    _UnitPrice = (decimal)data["UnitPrice"];
		//    _TaxableAmount = (decimal)data["TaxableAmount"];
		//    _ExtractCharge = (decimal)data["ExtractCharge"];
		//    _SenderDriverLicenseId = (string)data["SenderDriverLicenseId"];
		//    _Description = (string)data["Description"];
		//  }
		//  catch (Exception ex)
		//  {
		//    throw ex;
		//  }
		//}

		#endregion

		#region Save information into database
		// Save object data into database
		public void Add(Customer sender, Customer recepient, VienDongShipping obj)
		{
			ProviderFactory dataPF = new ProviderFactory();
			dataPF.BeginTransaction();
			try
			{
				CustomerModel customerModel = new CustomerModel();
				if (customerModel.IsExist(sender))
				{ customerModel.Update(dataPF, sender); }
				else
				{
					if (sender.Id == Guid.Empty)
						sender.Id = Guid.NewGuid();

					customerModel.Insert(dataPF, sender);
				}

				if (customerModel.IsExist(recepient))
				{ customerModel.Update(dataPF, recepient); }
				else
				{
					if (recepient.Id == Guid.Empty)
						recepient.Id = Guid.NewGuid();

					customerModel.Insert(dataPF, recepient);
				}

				obj.SenderId = sender.Id;
				obj.RecepientId = recepient.Id;
				obj.Id = Guid.NewGuid();

				Add(dataPF, obj);

				dataPF.CommitTransaction();
			}
			catch
			{
				dataPF.RollbackTransaction();
			}
			finally
			{
				dataPF.CloseConnection();
				dataPF.Dispose();
				dataPF = null;
			}
		}

		// Add new object data into database
		void Add(ProviderFactory dataPF, VienDongShipping obj)
		{

			// TO DO: Your validate code before adding new here!

			string insertSQL = @"INSERT INTO VienDongShipping (
														Id,
														DateCreated,
														SenderId,
														RecepientId,
														Created,
														CreatedId,
														Modified,
														ModifiedId,
														TrackNumber,
														ServiceType,
														TotalPakage,
														Height,
														Width,
														Length,
														DeclaredValue,
														InsuranceAmount,
														UnitPrice,
														TaxableAmount,
														ExtractCharge,
														SenderDriverLicenseId,
														Description
													) VALUES (
														@Id,
														@DateCreated,
														@SenderId,
														@RecepientId,
														@Created,
														@CreatedId,
														@Modified,
														@ModifiedId,
														@TrackNumber,
														@ServiceType,
														@TotalPakage,
														@Height,
														@Width,
														@Length,
														@DeclaredValue,
														@InsuranceAmount,
														@UnitPrice,
														@TaxableAmount,
														@ExtractCharge,
														@SenderDriverLicenseId,
														@Description
													)";
			IDbCommand command = dataPF.CreateCommand(insertSQL);

			command.Parameters.Add(dataPF.CreateParameter("@Id", DbType.StringFixedLength, Services.I.G2S(obj.Id)));
			command.Parameters.Add(dataPF.CreateParameter("@DateCreated", DbType.StringFixedLength, Services.I.DT2S(obj.DateCreated)));
			command.Parameters.Add(dataPF.CreateParameter("@SenderId", DbType.StringFixedLength, Services.I.G2S(obj.SenderId)));
			command.Parameters.Add(dataPF.CreateParameter("@RecepientId", DbType.StringFixedLength, Services.I.G2S(obj.RecepientId)));
			command.Parameters.Add(dataPF.CreateParameter("@Created", DbType.StringFixedLength, Services.I.DT2S(obj.Created)));
			command.Parameters.Add(dataPF.CreateParameter("@CreatedId", DbType.StringFixedLength, Services.I.G2S(obj.CreatedId)));
			command.Parameters.Add(dataPF.CreateParameter("@Modified", DbType.StringFixedLength, Services.I.DT2S(obj.Modified)));
			command.Parameters.Add(dataPF.CreateParameter("@ModifiedId", DbType.StringFixedLength, Services.I.G2S(obj.ModifiedId)));
			command.Parameters.Add(dataPF.CreateParameter("@TrackNumber", DbType.String, obj.TrackNumber));
			command.Parameters.Add(dataPF.CreateParameter("@ServiceType", DbType.Int16, obj.ServiceType));
			command.Parameters.Add(dataPF.CreateParameter("@TotalPakage", DbType.String, obj.TotalPakage));
			command.Parameters.Add(dataPF.CreateParameter("@Height", DbType.Int16, obj.Height));
			command.Parameters.Add(dataPF.CreateParameter("@Width", DbType.Int16, obj.Width));
			command.Parameters.Add(dataPF.CreateParameter("@Length", DbType.Int16, obj.Length));
			command.Parameters.Add(dataPF.CreateParameter("@DeclaredValue", DbType.Decimal, obj.DeclaredValue));
			command.Parameters.Add(dataPF.CreateParameter("@InsuranceAmount", DbType.Decimal, obj.InsuranceAmount));
			command.Parameters.Add(dataPF.CreateParameter("@UnitPrice", DbType.Decimal, obj.UnitPrice));
			command.Parameters.Add(dataPF.CreateParameter("@TaxableAmount", DbType.Decimal, obj.TaxableAmount));
			command.Parameters.Add(dataPF.CreateParameter("@ExtractCharge", DbType.Decimal, obj.ExtractCharge));
			command.Parameters.Add(dataPF.CreateParameter("@SenderDriverLicenseId", DbType.String, obj.SenderDriverLicenseId));
			command.Parameters.Add(dataPF.CreateParameter("@Description", DbType.String, obj.Description));

			try
			{
				command.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		// Update existing object data into database
		//    public void Update(ProviderFactory dataPF)
		//    {

		//      // TO DO: Your validate code before updating here!

		//      string updateSQL = @"UPDATE VienDongShippings SET 
		//														DateCreated=@DateCreated,
		//														SenderId=@SenderId,
		//														RecepientId=@RecepientId,
		//														Created=@Created,
		//														CreatedId=@CreatedId,
		//														Modified=@Modified,
		//														ModifiedId=@ModifiedId,
		//														TrackNumber=@TrackNumber,
		//														ServiceType=@ServiceType,
		//														TotalPakage=@TotalPakage,
		//														Height=@Height,
		//														Width=@Width,
		//														Length=@Length,
		//														DeclaredValue=@DeclaredValue,
		//														InsuranceAmount=@InsuranceAmount,
		//														UnitPrice=@UnitPrice,
		//														TaxableAmount=@TaxableAmount,
		//														ExtractCharge=@ExtractCharge,
		//														SenderDriverLicenseId=@SenderDriverLicenseId,
		//														Description=@Description
		//													WHERE Id=@Id";
		//      IDbCommand command = dataPF.CreateCommand(updateSQL);

		//      command.Parameters.Add(dataPF.CreateParameter("@DateCreated", DbType.DateTime, _DateCreated));
		//      command.Parameters.Add(dataPF.CreateParameter("@SenderId", DbType.Guid, _SenderId));
		//      command.Parameters.Add(dataPF.CreateParameter("@RecepientId", DbType.Guid, _RecepientId));
		//      command.Parameters.Add(dataPF.CreateParameter("@Created", DbType.DateTime, _Created));
		//      command.Parameters.Add(dataPF.CreateParameter("@CreatedId", DbType.Guid, _CreatedId));
		//      command.Parameters.Add(dataPF.CreateParameter("@Modified", DbType.DateTime, _Modified));
		//      command.Parameters.Add(dataPF.CreateParameter("@ModifiedId", DbType.Guid, _ModifiedId));
		//      command.Parameters.Add(dataPF.CreateParameter("@TrackNumber", DbType.String, _TrackNumber));
		//      command.Parameters.Add(dataPF.CreateParameter("@ServiceType", DbType.Int16, _ServiceType));
		//      command.Parameters.Add(dataPF.CreateParameter("@TotalPakage", DbType.String, _TotalPakage));
		//      command.Parameters.Add(dataPF.CreateParameter("@Height", DbType.Int16, _Height));
		//      command.Parameters.Add(dataPF.CreateParameter("@Width", DbType.Int16, _Width));
		//      command.Parameters.Add(dataPF.CreateParameter("@Length", DbType.Int16, _Length));
		//      command.Parameters.Add(dataPF.CreateParameter("@DeclaredValue", DbType.Decimal, _DeclaredValue));
		//      command.Parameters.Add(dataPF.CreateParameter("@InsuranceAmount", DbType.Decimal, _InsuranceAmount));
		//      command.Parameters.Add(dataPF.CreateParameter("@UnitPrice", DbType.Decimal, _UnitPrice));
		//      command.Parameters.Add(dataPF.CreateParameter("@TaxableAmount", DbType.Decimal, _TaxableAmount));
		//      command.Parameters.Add(dataPF.CreateParameter("@ExtractCharge", DbType.Decimal, _ExtractCharge));
		//      command.Parameters.Add(dataPF.CreateParameter("@SenderDriverLicenseId", DbType.String, _SenderDriverLicenseId));
		//      command.Parameters.Add(dataPF.CreateParameter("@Description", DbType.String, _Description));
		//      command.Parameters.Add(dataPF.CreateParameter("@Id", DbType.Guid, _Id));

		//      try
		//      {
		//        command.ExecuteNonQuery();
		//      }
		//      catch (Exception ex)
		//      {
		//        throw ex;
		//      }
		//    }

		#endregion

		#region Delete information from database
		// Delete object data from database
		public void Delete(Guid id)
		{
			ProviderFactory dataPF = new ProviderFactory();
			dataPF.OpenConnection();
			try
			{
				Delete(dataPF, id);
			}
			finally
			{
				dataPF.CloseConnection();
				dataPF.Dispose();
				dataPF = null;
			}
		}

		// Delete object data from database
		public void Delete(ProviderFactory dataPF, Guid id)
		{
			try
			{
				string deleteSQL = @"DELETE FROM VienDongShippings
														WHERE Id=@Id";
				IDbCommand command = dataPF.CreateCommand(deleteSQL);
				command.Parameters.Add(dataPF.CreateParameter("@Id", DbType.StringFixedLength, Services.I.G2S(id)));
				command.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		#endregion
	}
}
